<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Compare extends Controller_Index_Base
{    
    public function action_index()
    {
        $id1 = $this->request->param('id1') ? $this->request->param('id1') : 0;
        $id2 = $this->request->param('id2') ? $this->request->param('id2') : 0;
        // TODO проверить урл, чтобы нельзя было расплодить дубли страниц
        
        $model1 = ORM::factory('model', $id1);
        $model2 = ORM::factory('model', $id2);
         
        $model_type  = Model_Specs_Abstract::get_model_type($model1);
        $model_type2 = Model_Specs_Abstract::get_model_type($model2);
        if (!$model_type || !$model_type2 || ($model_type != $model_type2)) {
            // TODO нормальная страница 404
            die();    
        }         
        
        $competitors = ORM::factory('competitor')->merge_competitors($model1, $model2);
        
        
        $winner = ORM::factory('specs_'.$model_type)->get_winner($model1, $model2); 
        $compare_data = ORM::factory('specs_'.$model_type)->compare($model1, $model2, $competitors, $winner);
        
        if ($winner->id == $model1->id) {
            $winner = $model1;
            $second = $model2;   
        } else {
            $winner = $model2;
            $second = $model1;
        }
                          
        $content = new View('index/compare/index_tpl');
        $content->winner = $winner;
        $content->second = $second;
        $content->available_specs = ORM::factory('specs_'.$model_type)->get_available_specs(true);
        $content->winner_specs    = ORM::factory('specs_'.$model_type)->get_specs_full_data($winner, true);
        $content->second_specs    = ORM::factory('specs_'.$model_type)->get_specs_full_data($second, true);
        $content->compare_data = $compare_data;
        $content->competitors  = $competitors; 
        
        $description = Helper_Index::get_compare_index_description($model1, $model2);
        $this->print_template('Сравнение '.$model1->name.' и '.$model2->name, $content, $description); 
    }
} 
